package th.stripes.config;


import net.sourceforge.stripes.action.RedirectResolution;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.lang.StringUtils;
import th.model.Pengguna;
import th.stripes.BaseActionBean;
import th.stripes.WelcomeActionBean;


public class AuthRequiredIntereptor implements MethodInterceptor {
    
    private static String AWAM = "001";
    
    public Object invoke(MethodInvocation invocation) throws Throwable {
        BaseActionBean bean = (BaseActionBean) invocation.getThis();

        if (!bean.getContext().isLoggedIn()) {                       
            return new RedirectResolution(WelcomeActionBean.class)
                    .addParameter("message", "Sila LOG MASUK terlebih dahulu untuk menggunakan perkhidmatan ini.");
        } else {            
            Pengguna pengguna = bean.getContext().getCurrentUser();
            if (StringUtils.isBlank(pengguna.getNoPin()) && pengguna.getJenisPengguna().getKod().equals(AWAM)) {
                return new RedirectResolution(WelcomeActionBean.class)
                    .addParameter("message", "Sila dapatkan no pin di kaunter TH yang berdekatan.");
            }            
            String exp = "";
            try {
                return invocation.proceed();
            } catch (Exception ex) {
                exp = ex.getMessage();
                throw ex;
            } finally {
            }
        }
    }

}
